/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.timesoft.hibernate.core;

import com.timesoft.common.core.CoreContent;
import static com.timesoft.common.core.CoreContent.VALUE;
import com.timesoft.common.hibernate.TransactionalProcessor;
import com.timesoft.hibernate.bean.InformationSchemaColumns;
import java.util.HashMap;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author sorasaks
 */
public class InformationSchemaColumnsCore extends CoreContent {
    
    private static final Logger LOG = Logger.getLogger(InformationSchemaColumnsCore.class);
    
    public static InformationSchemaColumns getCharecterMaximumLengthByTableNameAndColumnName(final String tableName,
            final String columnName) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(InformationSchemaColumns.class);
                    criteria.add(Restrictions.eq("tableName", tableName));
                    criteria.add(Restrictions.eq("columnName", columnName));
                    criteria.setMaxResults(1);
                    
                    InformationSchemaColumns data = (InformationSchemaColumns)criteria.uniqueResult();
                    LOG.info("::::::::::::::: data :::::::::::::::" + data);

                    param.put(VALUE, data);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (InformationSchemaColumns) param.get(VALUE);
    }
    
}
